define('framework/databind/GillionDataBindDirectiveConstructor', ['angular'], function (angular) {

    return function ($compile, $parse, $timeout, $filter) {
        return {
            restrict: 'A',
            scope: {
                gDataBind: '=',
                valueProp:'@'
            },
            require: '^?ngModel',
            compile:function (tElement, tAttrs) {
                //tElement.find("input").attr('ng-change','associate.reSearch()');
                return function (scope, element, attrs, ngModel) {
                    scope.valuePropGetter = $parse(scope.valueProp);
                    scope.valueSeparator = attrs.valueSeparator || ',';//分隔符
                    scope.cascade = attrs.cascade || true;//是否级联
                    var valueText = '',
                        submitValue;
                    if(angular.isArray(scope.gDataBind)){
                        scope.$watchCollection("gDataBind",function(){
                            submitValue = [];
                            scope.gDataBind.forEach(function(item){
                                submitValue.push(scope.valuePropGetter(item));
                            });
                            ngModel.$setViewValue(submitValue.join(scope.valueSeparator));
                            ngModel.$render();
                        });
                    }else{
                        scope.$watch("gDataBind",function(){
                            ngModel.$setViewValue(scope.valuePropGetter(scope.gDataBind));
                            ngModel.$render();
                        });
                    }

                    scope.dupRemoval= function(rowDatas){

                        var result = [], hash = {},value;
                        for (var i = 0, elem; (elem = rowDatas[i]) != null; i++) {
                            value = scope.valuePropGetter(elem);
                            if (!hash[value]) {
                                result.push(elem);
                                hash[value] = true;
                            }
                        }
                        return result;
                    };
                    if(scope.cascade){
                        element.on('keyup', function (event) {
                            var e = e || event,
                                currKey = e.keyCode || e.which || e.charCode,
                                inputVal,keywordArr = [],selectedRow=[];

                            switch (currKey) {
                                //删除时触发
                                case 8:
                                    inputVal = element.val();
                                    if(inputVal){
                                        if(angular.isArray(scope.gDataBind)){
                                            keywordArr = inputVal.split(scope.valueSeparator);
                                            keywordArr.forEach(function(each){
                                                scope.gDataBind.forEach(function(item){
                                                    if(scope.valuePropGetter(item)==each){
                                                        selectedRow.push(item);
                                                    }
                                                })
                                            });
                                            selectedRow = scope.dupRemoval(selectedRow);
                                            scope.gDataBind = selectedRow;
                                        }else{
                                            if(scope.valuePropGetter(scope.gDataBind)!=inputVal){
                                                scope.gDataBind = undefined;
                                            }
                                        }
                                    }else{
                                        if(angular.isArray(scope.gDataBind)){
                                            scope.gDataBind = [];
                                        }else{
                                            scope.gDataBind = undefined;
                                        }
                                    }
                                    if(!scope.$root.$$phase){
                                        scope.$apply();
                                    }
                                    break;
                                default :

                                    break;
                            }
                        });
                    }
                }
            }
        };
    };
});